package com.drcoffee.drcoffeemall.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.drcoffee.drcoffeemall.entry.*;
import com.drcoffee.drcoffeemall.entry.erp.ErpImg;
import com.drcoffee.drcoffeemall.entry.ocm.HbaOCM;
import com.drcoffee.drcoffeemall.entry.ocm.OrderDetail4OCM;
import com.drcoffee.drcoffeemall.entry.ocm.OrderHeader4OCM;
import com.drcoffee.drcoffeemall.entry.select_list.OrderSelect;
import com.drcoffee.drcoffeemall.feign.EnMallProviderClient;
import com.drcoffee.drcoffeemall.feign.ErpProviderClient;
import com.drcoffee.drcoffeemall.feign.OcmProviderClient;
import com.drcoffee.drcoffeemall.mapper.OrderMapper;
import com.drcoffee.drcoffeemall.mapper.UserMapper;
import com.drcoffee.drcoffeemall.service.OrderService;
import com.drcoffee.drcoffeemall.service.WxBillService;
import com.drcoffee.drcoffeemall.util.BodyToMapUtil;
import com.drcoffee.drcoffeemall.util.SendWxWorkUtil;
import com.drcoffee.drcoffeemall.util.SfApiUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

@Service
@Transactional
//@DS("mysql")
@Slf4j
public class OrderServiceImpl implements OrderService {
    final
    OrderMapper orderMapper;
    final
    UserMapper userMapper;
    final
    SendWxWorkUtil sendWxWorkUtil;

    final
    PlatformTransactionManager transactionManager;

    final
    EnMallProviderClient enMallProviderClient;

    final
    OcmProviderClient ocmProviderClient;

    final
    ErpProviderClient erpProviderClient;

    final
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private WxBillService wxBillService;


    public OrderServiceImpl(OrderMapper orderMapper, UserMapper userMapper, SendWxWorkUtil sendWxWorkUtil, RedisTemplate<String, Object> redisTemplate, PlatformTransactionManager transactionManager, EnMallProviderClient enMallProviderClient, OcmProviderClient ocmProviderClient, ErpProviderClient erpProviderClient) {
        this.orderMapper = orderMapper;
        this.userMapper = userMapper;
        this.sendWxWorkUtil = sendWxWorkUtil;
        this.redisTemplate = redisTemplate;
        this.transactionManager = transactionManager;
        this.enMallProviderClient = enMallProviderClient;
        this.ocmProviderClient = ocmProviderClient;
        this.erpProviderClient = erpProviderClient;
    }

    @Override
    public List<Object> getNot(){
        return orderMapper.getNot();
    }

    @Override
    public Result tRecord() {
        //wxBillService.tRecord();
        wxBillService.getData();
//        wxBillService.tRecord();
//        wxBillService.aliPay();
        return null;
    }

    @Override
    public List<Main1> getMainData(Date date1, Date date2) {
        Integer count = orderMapper.getAllTradeCount(date1, date2);
        Integer enCount= enMallProviderClient.getAllTradeCount(date1, date2);
        List<Main1> list = new ArrayList<>();
        Main1 main = new Main1();
        main.setName("国内");
        main.setCount(count);
        list.add(main);

        Main1 main1 = new Main1();
        main1.setName("国外");
        main1.setCount(enCount);
        list.add(main1);
        return list;
    }


    @Override
    public ResponseEntity<Object> processAnOrder(String token, String order_code) {
        BsUser bsUser = (BsUser) redisTemplate.opsForValue().get(token);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("bs_user_id", bsUser.getId());
        paramMap.put("order_code", order_code);
        Map<String, Object> resultMap = enMallProviderClient.orderProcessing(paramMap);
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }


    @Override
    public ResponseEntity<Object> getNotThrowOrdderHead(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        paramMap.put("search", "%" + paramMap.get("search").toString() + "%");
        paramMap.put("selectKey", OrderSelect.valueOf(paramMap.get("select").toString()).getValue());
        List<Map<String, Object>> notThrowOrdder = orderMapper.getNotThrowOrdderHead(paramMap);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>(notThrowOrdder);
        return new ResponseEntity<>(resultData, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<Object> getNotThrowOrdderDetail(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> notThrowOrdder = orderMapper.getNotThrowOrdderDetail(paramMap);
        notThrowOrdder.forEach(System.out::println);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>(notThrowOrdder);
        return new ResponseEntity<>(resultData, HttpStatus.OK);
    }


    /*
     * 查询出未发货的订单
     * */
    @Override
    public ResponseEntity<Object> getTransportOrder(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> transport_order_list = orderMapper.getTransportOrder(paramMap);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>(transport_order_list);
        return new ResponseEntity<>(resultData, HttpStatus.OK);
    }

    @Override
    public Result setTransportCode(String order_code, String transport_code) {
        try {
            JSONObject jsonObject = SfApiUtil.codeValidityCheck(transport_code);
            Map<String, Object> codeValidityCheck = BodyToMapUtil.convertJsonToMap(String.valueOf(jsonObject));
            if ((boolean) codeValidityCheck.get("success")) {
                if (!(boolean) codeValidityCheck.get("msgData")) {
                    return new Result(2, "WARN", "物流单号校验未通过", null);
                } else {
                    orderMapper.setTransportCode(order_code, transport_code);
                    return new Result(1, "SUCCESS", "", null);
                }
            } else {
                return new Result(2, "ERROR", (String) codeValidityCheck.get("errorMsg"), null);
            }
        } catch (Exception e) {
            log.error("===================== setTransportCode()异常： =====================", e);
            return new Result(-1, "ERROR", e.getMessage(), null);
        }
    }

    @Override
    public ResponseEntity<Object> getVoucherData(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> voucher_list = orderMapper.getVoucherData(paramMap);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>(voucher_list);
        return new ResponseEntity<>(resultData, HttpStatus.OK);
    }

    @Override
    public Result setVoucherY(PayVoucher voucher) {
        try {
            orderMapper.setVoucherY(voucher);
            orderMapper.setOrderBackStatus(voucher.getOrder_code(), 3);
            orderMapper.setOrderStatus(voucher.getOrder_code(), 2, "status_time_02");
            return new Result(1, "SUCCESS", "", null);
        } catch (Exception e) {
            log.error("===================== setVoucherY()异常： =====================", e);
            return new Result(-1, "ERROR", e.getMessage(), null);
        }
    }

    @Override
    public ResponseEntity<Object> getOrderByCode(String orderCode) {
        Map<String, Object> order = orderMapper.getOrderByCode(orderCode);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("order_code", orderCode);
        paramMap.put("language", "CN");
        List<Map<String, Object>> part_list = orderMapper.getOrderParts(paramMap);
        order.put("part_list", part_list);
        return new ResponseEntity<>(order, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<Object> getAllTrade(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> trade_list = orderMapper.getAllTrade(paramMap);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>(trade_list);
        return new ResponseEntity<>(resultData, HttpStatus.OK);
    }


    @Override
    public Object getOrderList(Map<String, Object> paramMap) {

        System.out.println(paramMap);

        PageInfo<Map<String, Object>> resultMapPageInfo = null;
        Integer codeType = (Integer) paramMap.get("code_type");

        if (codeType == 3) {
            // 海外订单查询
            resultMapPageInfo = enMallProviderClient.getOrder(paramMap);
            return new ResponseEntity<Object>(resultMapPageInfo, HttpStatus.OK);
        }

        // 国内订单查询
        String code_prefix = "";
        if (codeType == 1) {
            // 个人用户PA
            code_prefix = "PA%";
        } else if (codeType == 2) {
            // 国内企业用户PB
            code_prefix = "PB%";
        } else if (codeType == 4) {
            // 个人用户(二类)PD
            code_prefix = "PD%";
        }
        paramMap.put("code_prefix", code_prefix);
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> order_list = orderMapper.getOrder(paramMap);
        resultMapPageInfo = new PageInfo<>(order_list);
        return new ResponseEntity<Object>(resultMapPageInfo, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<Object> getOrderParts(Map<String, Object> paramMap) {
        List<Map<String, Object>> orderPartList = new ArrayList<>();
        if (paramMap.get("language").equals("CN")) {
            orderPartList = orderMapper.getOrderParts(paramMap);
        } else {
            orderPartList = enMallProviderClient.getOrderParts(paramMap);
        }
        return new ResponseEntity<>(orderPartList, HttpStatus.OK);
    }


    @Override
    public ResponseEntity<Object> setOrderDisplace(Map<String, Object> paramMap) {
        Integer updates = orderMapper.setOrderDisplace(paramMap);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("updates", updates);
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

    @Override
    public ResponseEntity<Object> setOrderDisplace1(Map<String, Object> paramMap) {
        Integer updates = orderMapper.setOrderDisplace1(paramMap);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("updates", updates);
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    @Override
    public ResponseEntity<Object> throwToOcm(Map<String, Object> paramMap) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();

//        Map<String, Object> order = (Map<String, Object>) paramMap.get("order");
        String code = paramMap.get("code").toString();
        Map<String, Object> order = orderMapper.getOrderByToCode(code);
        List<Map<String, Object>> part_list = orderMapper.getOrderPartList(code);
        order.put("part_list", part_list);
        Date date = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyMMdd");
        SimpleDateFormat ft2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat ft3 = new SimpleDateFormat("yyyy-MM-dd");
        String orderCode = order.get("code").toString();

        OrderHeader4OCM header = new OrderHeader4OCM();
        // 渠道
//            String channelStr = order.getCode().substring(0, 2); //提取0-2位
//            if (channelStr.equals("PA")) {
//                header.setEaachnl("11");//PA:11 PB:12
//            } else if (channelStr.equals("PB")) {
//                header.setEaachnl("12");//PA:11 PB:12
//            }
        header.setEaasdocno(orderCode);
        header.setEaachnl("12");
        header.setEaadocno(orderCode);
        header.setEaaord(order.get("code").toString());
        header.setEaa038("3");

        String erpCode = order.get("erp_code").toString();
        header.setEaa083(erpCode);//账款客户编号
        header.setEaa084(erpCode);//业绩归属客户
        header.setEaatobj(erpCode);//渠道商
        header.setEaa053("45TF");//支付方式

        header.setEaaexco("N09F00031");//物流商
        header.setEaadept("CF9979");
        header.setEaabper("DR0292");
        header.setEaa002(ft3.format(date) + " 00:00:00");
        header.setEaatamt(order.get("order_total").toString());
        header.setEaa010(order.get("order_total").toString());
        header.setEaaerate("1");
        header.setEaacurr("RMB");
        header.setEaatax("S013");
        header.setEaatrate("13");
        header.setEaaddate(ft2.format(date));
        header.setEaamdate(ft2.format(date));
        header.setEaacdate(ft2.format(date));
        header.setEaa005(order.get("recipients").toString());
        header.setEaatel(order.get("phone").toString());
        header.setEaamtel(order.get("phone").toString());
        header.setEaaprv(order.get("province_code").toString());//省
        header.setEaaprv_n1(order.get("province").toString());
        header.setEaacity(order.get("city_code").toString());
        header.setEaacity_n1(order.get("city").toString());
        header.setEaadist(order.get("region_code").toString());
        header.setEaadist_n1(order.get("region").toString());
        header.setEaaaddr(order.get("address").toString());

        //默认审核
        header.setEaastus("Y");
        header.setEaaaper("admin");
        header.setEaaadate(ft2.format(date));
        if (order.get("province_codes") != null &&
                order.get("provinces") != null &&
                order.get("city_codes") != null &&
                order.get("citys") != null &&
                order.get("region_codes") != null &&
                order.get("regions") != null &&

                order.get("province_codes").equals("") &&
                order.get("provinces").equals("") &&
                order.get("city_codes").equals("") &&
                order.get("citys").equals("") &&
                order.get("region_codes").equals("") &&
                order.get("regions").equals("")

        ) {
            header.setEaaprv(order.get("province_codes").toString());
            header.setEaaprv_n1(order.get("provinces").toString());
            header.setEaacity(order.get("city_codes").toString());
            header.setEaacity_n1(order.get("citys").toString());
            header.setEaadist(order.get("region_codes").toString());
            header.setEaadist_n1(order.get("regions").toString());
        }


        List<Map<String, Object>> orderAttachments = (List<Map<String, Object>>) order.get("part_list");

        List<OrderDetail4OCM> detail_list = new ArrayList<>();
        for (Map<String, Object> orderAttachment : orderAttachments) {
            OrderDetail4OCM detail = new OrderDetail4OCM();
            if (orderAttachment.get("warehouse") != null && !orderAttachment.get("warehouse").toString().equals("")) {
                detail.setEabwh(orderAttachment.get("warehouse").toString());//货位
            }
            String part_code = orderAttachment.get("displace_code") == null || orderAttachment.get("displace_code").toString().equals("") ? orderAttachment.get("part_code").toString() : orderAttachment.get("displace_code").toString();


            detail.setEabdocno(orderCode);
            detail.setEab013(ft2.format(date));

            int seq = orderAttachments.indexOf(orderAttachment);
            detail.setEabord(orderAttachment.get("order_code").toString());
            seq++;
            detail.setEabseq(String.valueOf((seq)));
            detail.setEabsseq("1");
            detail.setEabmno(part_code);
            detail.setEabqty(
                    orderAttachment.get("displace_quantity").toString().equals("0")
                            ?
                            orderAttachment.get("quantity").toString()
                            :
                            orderAttachment.get("displace_quantity").toString()
            );

            Map<String, Object> paramMapCag = new HashMap<>();
            paramMapCag.put("caggoods", part_code);
            Map<String, Object> cagMap = ocmProviderClient.getCag(paramMapCag);
            if (cagMap == null) {
                throw new Exception("料号\"" + part_code + "\"在 OCM 中不存在");
            }
            detail.setEabgoods(part_code);
            System.out.println(cagMap);
            detail.setEabclass(cagMap.get("CAGCLASS").toString());
            detail.setEab027(cagMap.get("CAG008").toString());
            detail.setEabtunit((String) cagMap.get("CAGUNIT"));
            detail.setEabpunit((String) cagMap.get("CAGUNIT"));
            Map<String, Object> paramMapDan = new HashMap<>();
            paramMapDan.put("dangoods", part_code);
            paramMapDan.put("erp_code", order.get("erp_code").toString());
            Map<String, Object> danMap = ocmProviderClient.getDan(paramMapDan);

            if (danMap == null) {
                throw new Exception("未从 OCM 中查询到料号：\"" + part_code + "\" 的价格");
            } else {
                if (orderAttachment.get("is_giveaway").equals("Y")) {
                    detail.setEabprice("0");
                    detail.setEabtamt("0");
                    detail.setEabnamt("0");
                } else {
                    // 小计
                    BigDecimal total = new BigDecimal(orderAttachment.get("total").toString());//商城
                    total = total.setScale(2, RoundingMode.HALF_UP);
                    BigDecimal new_dan004 = (Integer) order.get("permission_id") == 1 ? new BigDecimal(String.valueOf(danMap.get("DANSPRICE"))) : new BigDecimal(String.valueOf(danMap.get("DAN004")));//OCM
                    new_dan004 = new_dan004.setScale(2, RoundingMode.HALF_UP);

                    BigDecimal part_quantity = new BigDecimal("0");

                    String displace_quantity = orderAttachment.get("displace_quantity").toString();
                    if (displace_quantity.equals("0")) {
                        part_quantity = new BigDecimal(orderAttachment.get("quantity").toString());
                    } else {
                        part_quantity = new BigDecimal(displace_quantity);
                    }

                    // OCM 价格
                    BigDecimal ocm_part_total = new_dan004.multiply(part_quantity);

                    if (!total.equals(ocm_part_total)) {
                        System.out.println(part_code);
                        throw new Exception("企业编号: " + erpCode + " ; 料号: " + part_code + " 的价格与 OCM 价格不一致 ; " +
                                "OCM价格: " + ocm_part_total + " ; 商城价格: " + total + " ;");
                    }
                    if ((Integer) order.get("permission_id") == 1) {//企业用户
                        detail.setEabprice(danMap.get("DANSPRICE").toString());//供价
                    } else {
                        //个人用户
                        detail.setEabprice(danMap.get("DAN004").toString());//常规售价
                    }
                    detail.setEabtamt(orderAttachment.get("total").toString());
                    detail.setEabnamt((BigDecimal.valueOf(Double.parseDouble(orderAttachment.get("total").toString())).divide(BigDecimal.valueOf(1.13), RoundingMode.HALF_UP)).toString());
                    detail.setEab100(danMap.get("DANDOCNO").toString());
                }
                detail.setEab002((danMap.get("DAN003").toString()));
                detail.setEabsprice(danMap.get("DANSPRICE").toString());
            }
            detail.setEab024("3");
            detail.setEab026("");
            if (part_code.equals("8H10014")) {
                detail.setEab026("GJCX2311140003");
                detail.setEab024("1");
                detail.setEab100("");
            } else if (part_code.equals("8H10015")) {
                detail.setEab026("GJCX2311140002");
                detail.setEab024("1");
                detail.setEab100("");
            } else if (part_code.equals("8H10016")) {
                detail.setEab026("CX2403060002");
                detail.setEab024("1");
                detail.setEab100("");
            }
            if (orderAttachment.get("is_giveaway").toString().equals("Y")) {
                detail.setEab024("2");
                detail.setEab100("");
            }

            detail.setEabexco("N09F00031");//顺丰
            detail.setEabaper("admin");
            detail.setEabadate(ft2.format(date));
            detail_list.add(detail);
        }
        Map<String, Object> paramMapToOcm = new HashMap<>();
        paramMapToOcm.put("header", header);
        paramMapToOcm.put("detail_list", detail_list);

        Map<String, Object> toOcmResult = ocmProviderClient.insertOcmOrder(paramMapToOcm);
        if (toOcmResult.get("status").equals("success")) {
            orderMapper.setFlipStatus(orderCode);
            sendWxWorkUtil.sendAppoint(2);
        }

        resultMap.put("ocm_code", orderCode);
        return new ResponseEntity<>(resultMap, HttpStatus.OK);
    }

//    @Scheduled(cron = "0/3 * * * * ?")
    public void get(){
        List<String> get = new ArrayList<>();
        get.add("DCXS2-220300004");
        get.add("DCXS2-220300006");
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("552204001","552204001");
//        System.out.println();
    }

//    @Scheduled(cron = "0 * * * * ?")
//    @PostConstruct
//    public void  init(){
//        this.scnz();
//        System.out.println("获取数值11111111111111111");
//    }
    @Scheduled(cron = "0 0 0 * * ?")
    public  void  scnz(){
        ArrayList<HbaOCM> ocm = ocmProviderClient.allOCM();
        System.out.println("获取到的已有的值的长度"+ocm.size());
        ArrayList<ErpImg> erp = erpProviderClient.allMaterial();
        System.out.println("获得将要插入的ERP的数据长度"+erp.size());
        if (ocm.size() > 0){
            int ss = ocmProviderClient.del();
            System.out.println("删除原OCM内的数据条数"+ss);
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter newDate = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowDate = now.format(newDate);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(nowDate);
            // 使用 Map 来存储合并后的数据
            Map<String, ErpImg> uniqueEntries = new HashMap<>();

            // 遍历 erp 列表，根据 img01 和 img02 进行合并
            for (ErpImg erpImg : erp) {
                String key = erpImg.getImg01() + "_" + erpImg.getImg02();

                // 如果 Map 中已存在相同的 img01 和 img02 的组合，则累加 img10 的值
                if (uniqueEntries.containsKey(key)) {
                    ErpImg existingErpImg = uniqueEntries.get(key);
                    BigDecimal currentImg10 = existingErpImg.getImg10();
                    existingErpImg.setImg10(currentImg10.add(erpImg.getImg10()));
                } else {
                    // 否则，将该实体类对象放入 Map 中
                    uniqueEntries.put(key, erpImg);
                }
            }

            // 将 Map 中的值转为 ArrayList，即为按要求合并后的结果
            ArrayList<ErpImg> mergedErp = new ArrayList<>(uniqueEntries.values());
            System.out.println("获取到了筛选后集合的数量"+mergedErp.size());
                for (ErpImg erpImg:mergedErp){
                    if ((erpImg.getImg10().compareTo(BigDecimal.ZERO) > 0)){
                        HbaOCM hbaOCM = new HbaOCM();
                        hbaOCM.setHbaent(662);
                        hbaOCM.setHbalorg("100001");
                        hbaOCM.setHbasloc("-");
                        hbaOCM.setHbabno("-");
                        hbaOCM.setHbawhchr("-");
                        hbaOCM.setHbagchr("-");
                        hbaOCM.setHbawh(erpImg.getImg02());
                        hbaOCM.setHbagoods(erpImg.getImg01());
                        hbaOCM.setHbaunit(erpImg.getImg09());
                        hbaOCM.setHbaqty(erpImg.getImg10());
                        hbaOCM.setHbaprice(BigDecimal.valueOf(0));
                        hbaOCM.setHbaedate(erpImg.getImg18());
                        hbaOCM.setHbastus("T");
                        hbaOCM.setHbalock(BigDecimal.valueOf(1));
                        hbaOCM.setHbacdate(date);
                        hbaOCM.setHbamdate(erpImg.getImg14());
                        hbaOCM.setHbacdept("CF9931");
                        hbaOCM.setHba001(erpImg.getImg10());
                        hbaOCM.setHbagstus("1");
                        ocmProviderClient.insertHBA(hbaOCM);
                    }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


//        Set<String> code = new HashSet<>();
//        Set<String> wareHouse = new HashSet<>();
//        Set<ErpImg> undiffrernt=new HashSet<>();
//
//        for (HbaOCM ocmItem : ocm){
//            code.add(ocmItem.getHbagoods());
//            wareHouse.add(ocmItem.getHbawh());
//        }
//        for (ErpImg erpImg: erp){
//            if (!code.contains(erpImg.getImg01())){
//                undiffrernt.add(erpImg);
//            }
//        }
//        for (HbaOCM OcmItem : ocm){
//            for (ErpImg erpImg:erp){
//                if (OcmItem.getHbagoods().equals(erpImg.getImg01()) && !wareHouse.contains(erpImg.getImg02())){
//                    undiffrernt.add(erpImg);
//                }
//            }
//        }
//        ArrayList<ErpImg> different = new ArrayList<>(undiffrernt);
//        ArrayList<ErpImg> allDifferent = new ArrayList<>();
//        int i = 0;
//        for (ErpImg erpImg : different){
//            allDifferent.add(erpProviderClient.different(erpImg.getImg01(),erpImg.getImg02()));
//             i +=i;
//        }

//        System.out.println("完整记录的数据条数"+i);
//        System.out.println("数据长度"+allDifferent.size());
//        System.out.println("获取到的不同数据如下所示"+different.size());

//        for (HbaOCM ocmItem : ocm) {
//            String ocmCode = ocmItem.getHbagoods();
//
//            for (ErpImg erpItem : erp) {
//                if (erpItem.getImg01()!=ocmCode){
//                    different.add(erpItem);
//                }
//                if (ocmCode.equals(erpItem.getImg01())) {
//                    if (!ocmItem.getHbawh().equals(erpItem.getImg02())) {
//                        different.add(erpItem);
//                    }
//                }
//            }
//        }

    }


}
